﻿# -*- coding: utf-8 -*-
'''eof
name:未结清贷款法人机构数（除住房按揭贷款）
code:UpaidLoanBranchCnt_2D
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:
eof'''


import sys
reload(sys)
sys.setdefaultencoding('utf-8')


def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0\
            or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return float(x)
        else:
            return False
    except:
        return False
def IC7_UpaidLoanBranchCnt():
    data = IC_DSJ_2D['data']
    organization = []
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        table1=getTable(data,"PDA",1,0)
        if table1 == 0:
            return 0
        elif table1==[{}]:
            return u"缺失值"
        else:
            PD01 = getTable(table1[0], 'PD01', 0,1)
            if PD01  == 0:
                return 0
            elif PD01 == [{}]:
                return u"缺失值"
            else:
                conts = 0
                for i in PD01:
                    try:
                        # 基本信息段
                        PD01A = getTable(i, 'PD01A', 1,1)
                        # 最新表现信息段
                        PD01B = getTable(i, 'PD01B', 1,1)
                        if PD01A == 0 or PD01B == 0:
                            pass
                        elif PD01A == [{}] or PD01B == [{}]:
                            conts += 1
                        else:
                            #账户类型
                            PD01AD01 = PD01A[0].get('PD01AD01')
                            #账户状态
                            PD01BD01 = PD01B[0].get('PD01BD01')
                            #余额
                            PD01BJ01 = PD01B[0].get('PD01BJ01')
                            #业务种类
                            PD01AD03 = PD01A[0].get('PD01AD03')
                            #管理机构代码
                            PD01AI02 = PD01A[0].get('PD01AI02')
                            if PD01AD01 in null_type_list or PD01BD01 in null_type_list or PD01BJ01 in null_type_list or PD01AD03 in null_type_list:
                                conts += 1
                            else:
                                if PD01AD01=="D1" and PD01BD01 in ['1','2','4','6','7','8'] and isfloat(PD01BJ01)>0 and PD01AD03 not in ['11','12','13'] and PD01AI02 not in null_type_list:
                                    organization.append(PD01AI02)
                                elif PD01AD01=="R1" and PD01BD01 in ['1','2','4','5','6','8'] and isfloat(PD01BJ01)>0 and PD01AD03 not in ['11','12','13'] and PD01AI02 not in null_type_list:
                                    organization.append(PD01AI02)
                                elif PD01AD01=="R4" and PD01BD01 in ['1','2','4','6','8'] and isfloat(PD01BJ01)>0 and PD01AD03 not in ['11','12','13'] and PD01AI02 not in null_type_list:
                                    organization.append(PD01AI02)
                                else:
                                    pass
                    except:
                        conts += 1
                if conts == len(PD01):
                    return u'缺失值'
                else:
                    return len(set(organization))
    except:
        return u'缺失值'

result = IC7_UpaidLoanBranchCnt()
